﻿//
//  State machine for managing button input from a phone interface
//	---passes key presses to current state 
//
//  Created by Codyo on 2008-07-05.
//  Copyright (c) 2008 Codyo. All rights reserved.
//

import com.codyo.mobileStateMachine.AbstractState;
//import com.codyo.mobileStateMachine.EntryState;
//import com.codyo.mobileStateMachine.ExitState;
import com.codyo.states.EntryState;
import com.codyo.states.GameInitState;
import com.codyo.states.GamePlayState;
import com.codyo.states.GameResponseState;
import com.codyo.states.ExitState;
//import com.codyo.data.ProblemSet;

class com.codyo.mobileStateMachine.MobileStateMachine {
	
	public static var ENTRY_STATE:String = "entry";
	public static var GAME_INIT_STATE:String = "gameInit";
	public static var GAME_PLAY_STATE:String = "gamePlay";
	public static var GAME_RESPONSE_STATE:String = "gameResponse";
	public static var EXIT_STATE:String = "exit";
	
	public var STAGE_REF:MovieClip;
	
	private var state:AbstractState;
	private var keyListener:Object = new Object();
	
	public function MobileStateMachine(theStage){
		trace("mobile state engine created")
		//setState()
		setState(MobileStateMachine.ENTRY_STATE);
		//setState(MobileStateMachine.EXIT_STATE);
		createKeyListener();
		_level0.theStateEngine = this;
		STAGE_REF = theStage;
	}
	
	public function getStage(){
		return(STAGE_REF);
	}
	
	
	//functions to listen to keypress's and pass along announcement
	public function createKeyListener(){
		keyListener.onKeyDown = function() {
			// Compare return value of getCode() to constant
			switch(Key.getCode()){
				case "soft1":
					_level0.theStateEngine.state.keypadPressed("keySoft1");
					break;
				case "soft2":
					_level0.theStateEngine.state.keypadPressed("keySoft2");
					break;
				case 49:	//key 1
					_level0.theStateEngine.state.keypadPressed("key1");
					break;
				case 50:	//key 2
					_level0.theStateEngine.state.keypadPressed("key2");
					break;
				case 51:	//key 3
					_level0.theStateEngine.state.keypadPressed("key3");
					break;
				case 52:	//key 4
					_level0.theStateEngine.state.keypadPressed("key4");
					break;
				case 53:	//key 5
					_level0.theStateEngine.state.keypadPressed("key5");
					break;
				case 54:	//key 6
					_level0.theStateEngine.state.keypadPressed("key6");
					break;
				case 55:	//key 7
					_level0.theStateEngine.state.keypadPressed("key7");
					break;
				case 56:	//key 8
					_level0.theStateEngine.state.keypadPressed("key8");
					break;
				case 57:	//key 9
					_level0.theStateEngine.state.keypadPressed("key9");
					break;
				case 48:	//key 0
					_level0.theStateEngine.state.keypadPressed("key0");
					break;
				case 106:	//key *
					_level0.theStateEngine.state.keypadPressed("keyStar");
					break;
				case 16:	//key #
					_level0.theStateEngine.state.keypadPressed("keyHash");
					break;
				case 13:	//key directional select
					_level0.theStateEngine.state.keypadPressed("select");
					break;
				case 37:	//key left
					_level0.theStateEngine.state.keypadPressed("left");
					break;
				case 38:	//key up
					_level0.theStateEngine.state.keypadPressed("up");
					break;
				case 39:	//key right
					_level0.theStateEngine.state.keypadPressed("right");
					break;
				case 40:	//key down
					_level0.theStateEngine.state.keypadPressed("down");
					break;
				default:
					_level0.theStateEngine.state.keypadPressed(Key.getCode());
					break;
			}
		
		};
		Key.addListener(keyListener);
	}
	
	public function setState(theState:String):Void{
		switch(theState){
			case ENTRY_STATE:
				state = new EntryState(this);
				break;
			case GAME_INIT_STATE:
				state = new GameInitState(this);
				break;
			case GAME_PLAY_STATE:
				state = new GamePlayState(this);
				break;
			case GAME_RESPONSE_STATE:
				state = new GameResponseState(this);
				break;
			case EXIT_STATE:
				state = new ExitState(this);
				break;
			default:
				state = null;			
		}
		
	}
}